package ywpt.web.action.client;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import ywpt.core.SystemMessage;
import ywpt.core.exception.UniqueConstraintException;
import ywpt.core.web.BaseAction;
import ywpt.model.ClientLinkman;
import ywpt.service.client.ClientLinkmanService;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

public class ClientLinkmanAction extends BaseAction implements ModelDriven<ClientLinkman>, Preparable{

	/**
	 * 客户联系人Action
	 */
	private static final long serialVersionUID = 8458733767265642046L;
	private Long id;
	private Long clientId;
	private Long typeId;
	private String typeName;
	private ClientLinkman clientLinkman;
	private List<ClientLinkman> clientLinkmanList = new ArrayList<ClientLinkman>();
	private ClientLinkmanService clientLinkmanService;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Long getClientId() {
		return clientId;
	}

	public void setClientId(Long clientId) {
		this.clientId = clientId;
	}
	public Long getTypeId() {
		return typeId;
	}

	public void setTypeId(Long typeId) {
		this.typeId = typeId;
	}

	public String getTypeName() {
		return typeName;
	}

	public void setTypeName(String typeName) {
		this.typeName = typeName;
	}
	public ClientLinkman getClientLinkman() {
		return clientLinkman;
	}

	public void setClientLinkman(ClientLinkman clientLinkman) {
		this.clientLinkman = clientLinkman;
	}

	public ClientLinkmanService getClientLinkmanService() {
		return clientLinkmanService;
	}

	public void setClientLinkmanService(ClientLinkmanService clientLinkmanService) {
		this.clientLinkmanService = clientLinkmanService;
	}

	@Override
	public ClientLinkman getModel() {
		return this.clientLinkman;
	}

	@Override
	public void prepare() throws Exception {
		if( id != null){
			this.clientLinkman = this.clientLinkmanService.getClientLinkmanById(id);
		}else{
			this.clientLinkman = new ClientLinkman();
		}
	}
	
	public String input(){	
		this.setClientId(clientId);
		return INPUT;
	} 

	public String edit(){
		this.clientLinkman = this.clientLinkmanService.getClientLinkmanById(id);
		if(clientLinkman.getClient() != null){
			this.setClientId(clientLinkman.getClient().getId());
		}
		if(clientLinkman.getType() != null){
			this.setTypeId(clientLinkman.getType().getId());
			this.setTypeName(clientLinkman.getType().getCodeName());
		}
		return INPUT;
	}
	/**
	 * 
	 * @return
	 */
	public String save() {
		try {
			if (id == null) {
				clientLinkmanService.saveClientLinkman(clientLinkman ,clientId,typeId);
				this.addActionMessage(SystemMessage.SAVE_SUCCESS);
			} else {
				clientLinkmanService.updateClientLinkman(clientLinkman,clientId,typeId);
				this.addActionMessage(SystemMessage.UPDATE_SUCCESS);
			}
			return SUCCESS;
		} catch (UniqueConstraintException e) {
			this.addActionError(SystemMessage.SAVE_FAIL + e.getMessage());
			return INPUT;
		}
	}
	 public String delete() throws Exception {
		if (id != null) 
		{	
			 clientLinkmanService.removeClientLinkman(id);
			return SUCCESS;				
		}else {
			this.addActionMessage(SystemMessage.NO_OBJECT);
			return INPUT;
		}
	}

		public String select()
		{
			DetachedCriteria query=DetachedCriteria.forClass(ClientLinkman.class);		
			if(clientId!=null){
				query.createAlias("client", "client").add(Restrictions.eq("client.id", clientId));
			}
			clientLinkmanList.addAll(clientLinkmanService.getClientLinkmans(query));			
			return SUCCESS;
		}
	 
	 
	 
	public List<ClientLinkman> getClientLinkmanList() {
		return clientLinkmanList;
	}

	public void setClientLinkmanList(List<ClientLinkman> clientLinkmanList) {
		this.clientLinkmanList = clientLinkmanList;
	}



}
